package com.superhero.website.controller;

import com.alibaba.fastjson.JSONObject;
import com.superhero.common.pojo.ApiResult;
import com.superhero.common.pojo.vo.website.LoginOutput;
import com.superhero.website.service.LoginService;
import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;


@Api(tags = {"登录注册"})
@RestController
@RequestMapping("/website/auth")
public class LoginController {

    @Resource
    LoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    private ApiResult<LoginOutput> login(@ApiParam(name = "username") @NotNull String username,
                                         @ApiParam(name = "password") @NotNull String password) {
        return ApiResult.success(loginService.login(username, password));
    }

    // @ApiOperation(value = "修改密码")
    // @PutMapping("/password")
    // private ApiResult changePassword(@ApiParam(name = "oldPassword") @NotNull String oldPassword,
    //                                  @ApiParam(name = "newPassword") @NotNull String newPassword) {
    //     loginService.changePassword(oldPassword, newPassword);
    //     return ApiResult.success();
    // }

    @ApiOperation(value = "腾讯防水墙核查验证码票据结果", notes = "接口返回resultCode为0时正常，为-1时时失败，resultMsg会写明原因")
    @GetMapping("/describeCaptchaResult")
    private ApiResult<JSONObject> describeCaptchaResult(@ApiParam(name = "ticket") @NotNull String ticket,
                                                        @ApiParam(name = "randstr") @NotNull String randstr) throws TencentCloudSDKException {
        JSONObject result = new JSONObject();

        Credential cred = new Credential("AKIDRfRxGIaC4eosUqGkVjkUxhBSUWHmu70o", "wV6RVozwdCbN0PZYHCyjXijeF3c0LSJv");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("captcha.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        CaptchaClient client = new CaptchaClient(cred, "", clientProfile);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Action", "DescribeCaptchaResult");
        jsonObject.put("Version", "2019-07-22");
        jsonObject.put("CaptchaType", "9");
        jsonObject.put("Ticket", ticket);
        jsonObject.put("UserIp", "127.0.0.1");
        jsonObject.put("Randstr", randstr);
        jsonObject.put("CaptchaAppId", "2035692978");
        jsonObject.put("AppSecretKey", "0Rn_68yklszq5NRzeKQqiSQ**");
        DescribeCaptchaResultRequest req = DescribeCaptchaResultRequest.fromJsonString(jsonObject.toJSONString(), DescribeCaptchaResultRequest.class);

        DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);
        if (resp.getCaptchaCode() != 1) {
            return ApiResult.error(-1, resp.getCaptchaMsg());
        }
        return ApiResult.success();
    }
}
